﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common.reflection;
using net.veekee.logprocessor.plugin.output;
using net.veekee.common;
using net.veekee.logprocessor.plugin.input;

namespace net.veekee.logprocessor.plugin.input.pack
{
    public class InputHandler : ITypeListHandler
    {
        public void OnRegister()
        {
            InputFactory.Register("csv", typeof(CSVProcessor).FullName);
            Options.GetInstance().Add(new Option() { Name = "input.log.pattern", MandatoryValidation = new MandatoryOptionValidatorHandler(InputHandler.MandatoryOptionValidatorMakesItDepends), NoArgument = false, Description = "Set the RegEx pattern used to decompose logs", DefaultValue = "(?<Message>.*)" });
            Options.GetInstance().Add(new Option() { Name = "output.csv.separator", MandatoryValidation = new MandatoryOptionValidatorHandler(InputHandler.MandatoryOptionValidatorMakesItDepends), NoArgument = false, Description = "Set the CSV output file separator", DefaultValue = ";" });

            InputFactory.Register("xml", typeof(XMLProcessor).FullName);
            Options.GetInstance().Add(new Option() { Name = "input.xml.tag", MandatoryValidation = new MandatoryOptionValidatorHandler(InputHandler.MandatoryOptionValidatorMakesItDepends), DefaultValue = "", Description = "Tag name used to parse XML element" });

            InputFactory.Register("log", typeof(LogFileProcessor).FullName);
            Options.GetInstance().Add(new Option() { Name = "buffer.maxsize", MandatoryValidation = new MandatoryOptionValidatorHandler(OptionValidatorHandler.IsNotMandatory), NoArgument = false, Description = "Max line size", DefaultValue = "999999" });
        }

        private static bool MandatoryOptionValidatorMakesItDepends(object sender, params Object[] args)
        {
            switch (sender.ToString())
            {
                case "input.log.pattern":
                    return (Options.GetInstance().Get("input.format") == "csv");
                case "buffer.maxsize":
                    return (Options.GetInstance().Get("input.format") == "log");
                case "input.xml.tag":
                    return (Options.GetInstance().Get("input.format") == "xml");
            }
            return false;
        }
    }
}
